package GUI;


import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.util.Vector;

import javax.swing.*;
import javax.swing.plaf.FileChooserUI;

import client.ClientConsole;

import Library.Books2;

import java.awt.Button;
import java.awt.event.AdjustmentListener;
import java.awt.event.AdjustmentEvent;

/**this class get new boom details and send it to server to add this book to db*/
public class GUI_AddBook extends JPanel
{

	private JTextArea log;
	private JTextField textFieldID;
	private JTextField textFieldName;
	private JTextField textFieldAuthor;
	private JTextField textFieldEdition;
	private JTextField textFieldDateOfPrint;
	private JTextField textFieldScope;
	private JTextField textFieldDescription;
	private JTextField textFieldCatalogNum;
	private JTextField textFieldNumOfCopies;
	private JTextField textFieldPurchaseDate;
	private JTextField textFieldLocationOnShelf;
	private JTextField textFieldRank;
	/**file file to save table of content*/
	private File file;
	/**clc instance of ClientConsole*/
	private ClientConsole clc;
	
	/**@param cc current ClientConsole
	 * @param gl instance of current GUI_LogIn
	 * @param gc instance of current GUI_Catalog*/
	public GUI_AddBook(ClientConsole cc,final GUI_LogIn gl,final GUI_Catalog gc)
	{
		clc=cc;
		setLayout(null);
		this.setSize(700, 600);
		this.setName("Add Book");
		JLabel lblId = new JLabel("ID");
		lblId.setBounds(22, 56, 24, 14);
		add(lblId);
		
		textFieldID = new JTextField();
		textFieldID.setBounds(239, 50, 231, 20);
		add(textFieldID);
		textFieldID.setColumns(10);
		
		JLabel lblName = new JLabel("Name");
		lblName.setBounds(22, 81, 46, 14);
		add(lblName);
		
		textFieldName = new JTextField();
		textFieldName.setBounds(239, 78, 231, 20);
		add(textFieldName);
		textFieldName.setColumns(10);
		
		JLabel lblAuthor = new JLabel("Author");
		lblAuthor.setBounds(22, 106, 39, 14);
		add(lblAuthor);
		
		textFieldAuthor = new JTextField();
		textFieldAuthor.setBounds(239, 103, 231, 20);
		add(textFieldAuthor);
		textFieldAuthor.setColumns(10);
		
		JLabel lblEdition = new JLabel("Edition");
		lblEdition.setBounds(22, 131, 46, 14);
		add(lblEdition);
		
		textFieldEdition = new JTextField();
		textFieldEdition.setBounds(239, 128, 231, 20);
		add(textFieldEdition);
		textFieldEdition.setColumns(10);
		
		JLabel lblDateOfPrint = new JLabel("Date Of Print");
		lblDateOfPrint.setBounds(22, 156, 121, 23);
		add(lblDateOfPrint);
		
		textFieldDateOfPrint = new JTextField();
		textFieldDateOfPrint.setBounds(239, 157, 231, 20);
		add(textFieldDateOfPrint);
		textFieldDateOfPrint.setColumns(10);
		
		JLabel lblScope = new JLabel("Scope");
		lblScope.setBounds(22, 190, 46, 14);
		add(lblScope);
		
		textFieldScope = new JTextField();
		textFieldScope.setBounds(239, 187, 231, 20);
		add(textFieldScope);
		textFieldScope.setColumns(10);
		
		JLabel lblDescription = new JLabel("Description");
		lblDescription.setBounds(22, 215, 121, 20);
		add(lblDescription);
		
		JLabel lblCatalogNumber = new JLabel("Catalog Number");
		lblCatalogNumber.setBounds(22, 246, 141, 21);
		add(lblCatalogNumber);
		
		JLabel lblNumberOfCopies = new JLabel("Number Of Copies");
		lblNumberOfCopies.setBounds(22, 278, 141, 20);
		add(lblNumberOfCopies);
		
		JLabel lblPurchaseDate = new JLabel("Purchase Date");
		lblPurchaseDate.setBounds(22, 309, 141, 20);
		add(lblPurchaseDate);
		
		JLabel lblLocationOnShelf = new JLabel("Location On Shelf");
		lblLocationOnShelf.setBounds(22, 340, 121, 23);
		add(lblLocationOnShelf);
		
		textFieldDescription = new JTextField();
		textFieldDescription.setBounds(239, 215, 231, 20);
		add(textFieldDescription);
		textFieldDescription.setColumns(10);
		
		textFieldCatalogNum = new JTextField();
		textFieldCatalogNum.setBounds(239, 247, 231, 20);
		add(textFieldCatalogNum);
		textFieldCatalogNum.setColumns(10);
		
		textFieldNumOfCopies = new JTextField();
		textFieldNumOfCopies.setBounds(239, 278, 231, 20);
		add(textFieldNumOfCopies);
		textFieldNumOfCopies.setColumns(10);
		
		textFieldPurchaseDate = new JTextField();
		textFieldPurchaseDate.setBounds(239, 309, 231, 20);
		add(textFieldPurchaseDate);
		textFieldPurchaseDate.setColumns(10);
		
		textFieldLocationOnShelf = new JTextField();
		textFieldLocationOnShelf.setBounds(239, 341, 231, 20);
		add(textFieldLocationOnShelf);
		textFieldLocationOnShelf.setColumns(10);
		
		JLabel lblRank = new JLabel("Rank");
		lblRank.setBounds(22, 374, 66, 14);
		add(lblRank);
		
		textFieldRank = new JTextField();
		textFieldRank.setBounds(239, 371, 231, 20);
		add(textFieldRank);
		textFieldRank.setColumns(10);
		
		JButton btnAdd = new JButton("Add");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				try {
					String id=textFieldID.getText();
					String name=textFieldName.getText();
					String author=textFieldAuthor.getText();
					String edition=textFieldEdition.getText();
					String printDate=textFieldDateOfPrint.getText();
					String scope=textFieldScope.getText();
					String desc=textFieldDescription.getText();
					String cNum=textFieldCatalogNum.getText();
					String numCopies=textFieldNumOfCopies.getText();
					String purDate=textFieldPurchaseDate.getText();
					String location=textFieldLocationOnShelf.getText();
					String rank=textFieldRank.getText();
					String path="";
					
					byte[] bytes={};
					if(file!=null)
					{
						 InputStream inputStream = new FileInputStream(file);
						 bytes = new byte[(int) file.length()];
						 inputStream.read(bytes);
						 //FileBytes fb=new FileBytes(bytes);
						 path=file.getAbsolutePath();
					}
					 
					 
					if(id.equals("")||name.equals("")||author.equals("")||edition.equals("")||printDate.equals("")||scope.equals("")||desc.equals("")||cNum.equals("")||numCopies.equals("")||purDate.equals("")||location.equals("")||rank.equals("")||(bytes.length<=0))
					{
						JOptionPane.showMessageDialog(new JFrame(),"Please fill all fields");
						return;
					}
					Books2 book=new Books2(Integer.parseInt(id),name,author,Integer.parseInt(edition),printDate,scope,desc,Integer.parseInt(cNum),Integer.parseInt(numCopies),purDate,Integer.parseInt(location),Integer.parseInt(rank),bytes);
					clc.client.sendToServer(book);
					while(clc.client.flag==0)
						System.out.print("");
					clc.client.flag=0;
					JOptionPane.showMessageDialog(new JFrame(),clc.client.message);
					
					textFieldID.setText("");
					textFieldName.setText("");
					textFieldAuthor.setText("");
					textFieldEdition.setText("");
					textFieldDateOfPrint.setText("");
					textFieldScope.setText("");
					textFieldDescription.setText("");
					textFieldCatalogNum.setText("");
					textFieldNumOfCopies.setText("");
					textFieldPurchaseDate.setText("");
					textFieldLocationOnShelf.setText("");
					textFieldRank.setText("");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btnAdd.setBounds(302, 451, 89, 23);
		add(btnAdd);
		
		JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gl.setContentPane(gc);
			}
		});
		btnBack.setBounds(417, 451, 89, 23);
		add(btnBack);
		
		JButton btnLogOut = new JButton("Log Out");
		btnLogOut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				gl.LogOut();
			}
		});
		btnLogOut.setBounds(529, 451, 89, 23);
		add(btnLogOut);
		
		JLabel lblPleaseFillAll = new JLabel("Please fill all the fields:");
		lblPleaseFillAll.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblPleaseFillAll.setBounds(22, 11, 220, 34);
		add(lblPleaseFillAll);
		
		JLabel lblNewLabel = new JLabel("table of contents");
		lblNewLabel.setBounds(22, 413, 109, 14);
		add(lblNewLabel);
		
		
		
		final JFileChooser fc = new JFileChooser();
		Button button = new Button("Browse");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int returnVal = fc.showOpenDialog(GUI_AddBook.this);
				 
	            if (returnVal == JFileChooser.APPROVE_OPTION) {
	                 file = fc.getSelectedFile();
	          
	              
	               
	            }
			}
		});
		
		
		button.setBounds(239, 408, 69, 19);
		add(button);
		this.setVisible(true);
	}

}